package com.ruoyi.environment.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.environment.domain.OpmEnvInspect;
import com.ruoyi.environment.mapper.OpmEnvInspectMapper;
import com.ruoyi.environment.service.IOpmEnvInspectService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 环保巡检Service业务层处理
 *
 * @author cjie
 * @date 2021-07-23
 */
@Service
public class OpmEnvInspectServiceImpl extends LinkFileServiceImpl implements IOpmEnvInspectService {
    @Autowired
    private OpmEnvInspectMapper opmEnvInspectMapper;

    /**
     * 查询环保巡检
     *
     * @param id 环保巡检ID
     * @return 环保巡检
     */
    @Override
    public OpmEnvInspect selectOpmEnvInspectById(Long id) {
        OpmEnvInspect result = opmEnvInspectMapper.selectOpmEnvInspectById(id);
        result.setFileList(getFileList(id.toString()));
        return result;
    }

    /**
     * 查询环保巡检列表
     *
     * @param opmEnvInspect 环保巡检
     * @return 环保巡检
     */
    @Override
    public List<OpmEnvInspect> selectOpmEnvInspectList(OpmEnvInspect opmEnvInspect) {
        return opmEnvInspectMapper.selectOpmEnvInspectList(opmEnvInspect);
    }

    /**
     * 新增环保巡检
     *
     * @param opmEnvInspect 环保巡检
     * @return 结果
     */
    @Override
    public int insertOpmEnvInspect(OpmEnvInspect opmEnvInspect) {
        opmEnvInspect.setId(IdUtil.getSnowflake(0, 0).nextId());
        opmEnvInspect.setCreateTime(DateUtils.getNowDate());
        opmEnvInspect.setCreateBy(SecurityUtils.getUsername());
        linkFile(opmEnvInspect.getFileIdList(), opmEnvInspect.getId().toString());
        return opmEnvInspectMapper.insertOpmEnvInspect(opmEnvInspect);
    }

    /**
     * 修改环保巡检
     *
     * @param opmEnvInspect 环保巡检
     * @return 结果
     */
    @Override
    public int updateOpmEnvInspect(OpmEnvInspect opmEnvInspect) {
        opmEnvInspect.setUpdateTime(DateUtils.getNowDate());
        opmEnvInspect.setUpdateBy(SecurityUtils.getUsername());
        linkFile(opmEnvInspect.getFileIdList(),Long.toString(opmEnvInspect.getId()));
        return opmEnvInspectMapper.updateOpmEnvInspect(opmEnvInspect);
    }

    /**
     * 批量删除环保巡检
     *
     * @param ids 需要删除的环保巡检ID
     * @return 结果
     */
    @Override
    public int deleteOpmEnvInspectByIds(Long[] ids) {
        String[] res = new String[ids.length];
        for(int i=0;i<ids.length;i++){
            res[i] = String.valueOf(ids[i]);
        }
        deleteLinkFile(res);
        return opmEnvInspectMapper.deleteOpmEnvInspectByIds(ids);
    }

    /**
     * 删除环保巡检信息
     *
     * @param id 环保巡检ID
     * @return 结果
     */
    @Override
    public int deleteOpmEnvInspectById(Long id) {
        //deleteLinkFile(id.toString());
        return opmEnvInspectMapper.deleteOpmEnvInspectById(id);
    }
}
